﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System;

namespace GogTasks.UserControls
{

    public partial class ShadedButton : UserControl
    {
        private bool _pressed;
        
        private Color _color1Pressed;
        private Color _color2Pressed;

        private Color _color1;
        private Color _color2;

        //[DefaultValue("Gray")]
        /// <summary>
        /// Background color at the upper half of the buttom
        /// </summary>
        public Color Color1
        {
            get
            {
                return _color1;
            }
            set
            {
                if (_color1 != value)
                {
                    _color1 = value;
                    _color1Pressed = LightenOrDarken(_color1);
                }
            }
        }
        //[DefaultValue("LightGray")]
        /// <summary>
        /// Background color at the bottom half of the buttom
        /// </summary>
        public Color Color2
        {
            get
            {
                return _color2;
            }
            set
            {
                if (_color2 != value)
                {
                    _color2 = value;
                    _color2Pressed = LightenOrDarken(_color2);
                    InitColors();
                }
            }
        }

        /// <summary>
        /// A number from 1-255 representing the contrast between pressed and unpressed state.
        /// </summary>
        public int ButtonPressContrast { set; get; }

        [DefaultValue("Button")]
        /// <summary>
        /// Mirror of property Text
        /// </summary>
        public string TextCopy
        {
            get { return Text; }
            set
            {
                Text = value;
                //Refresh();
                Invalidate();
            }
        }

        /// <summary>
        /// Enable/disables button
        /// 
        /// </summary>
        public new bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                if (base.Enabled != value)
                {
                    base.Enabled = value;
                    //Refresh();

                    Invalidate();
                }
            }
        }

        public new string Text { get; set; }

        #region Business methods
        public ShadedButton()
        {
            InitializeComponent();
            ButtonPressContrast = 40;
            TextCopy = "Button";
            InitColors();
            TextChanged += ShadedButton_TextChanged;
            EnabledChanged += delegate { Invalidate();};
        }

        private void InitColors()
        {
            if (IsDisposed) return;
           
            ForeColor = Color.Black;
            Color1 = Color.Gray;
            Color2 = Color.LightGray;
            _color1Pressed = LightenOrDarken(Color1);
            _color2Pressed = LightenOrDarken(Color2);
        }

        public override void Refresh()
        {
            InitColors();
            base.Refresh();
        }

        void ShadedButton_TextChanged(object sender, System.EventArgs e)
        {
            Update();
        }

        private static Color LightenOrDarken(Color color)
        {
            Color retCol = Color.Black;
            try
            {
                //int strength = ButtonPressContrast;
                int strength = 40;
                int r, g, b;
                if (color.R + color.G + color.B > 128 * 3)
                {
                    // darken
                    r = color.R - strength;
                    g = color.G - strength;
                    b = color.B - strength;
                    if (r < 0) r = 0;
                    if (g < 0) g = 0;
                    if (b < 0) b = 0;
                }
                else
                {
                    // Lighten
                    r = color.R + strength;
                    g = color.G + strength;
                    b = color.B + strength;
                    if (r > 255) r = 255;
                    if (g > 255) g = 255;
                    if (b > 255) b = 255;
                
                }
                retCol= Color.FromArgb(r, g, b);
            }
            catch (Exception){}
            return retCol;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            InitColors();

            //IntPtr iptr = e.Graphics.GetHdc();
            //var gPlus = new GraphicsPlus(iptr);
            var col1 = Color1;
            var col2 = Color2;
            var graf = e.Graphics;
            if (_pressed)
            {
                col1 = _color1Pressed;
                col2 = _color2Pressed;
            }
            int midY = Height / 2;
            var br1 = new SolidBrush(col1);
            var br2 = new SolidBrush(col2);
            graf.FillRectangle(br1, 0, 0, Width, midY);
            graf.FillRectangle(br2, 0, midY, Width, Height);

            SizeF textSize = graf.MeasureString(Text, Font);
            Color fontColor = ForeColor;
            if (!Enabled) fontColor = Color.Gray;
            graf.DrawString(Text, Font, new SolidBrush(fontColor),
                (Width - textSize.Width) / 2,
                (Height - textSize.Height) / 2);

        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
        #endregion

        #region Event listeners
        private void ShadedButton_MouseDown(object sender, MouseEventArgs e)
        {
            _pressed = true;
            //Refresh();
            Invalidate();
        }

        private void ShadedButton_MouseUp(object sender, MouseEventArgs e)
        {
            _pressed = false;
            //Refresh();
            Invalidate();
        }
        #endregion
    }
}
